var $pointrule= (function(){
	var host = pageMsg.host;
    var currentPageIndex = 0; //保存当前页码数
    var defaultIndex = 1;//默认页码数
    var pageSize = 12;//默认请求size
    var keyword = ""; //保存当前搜索关键词
	
	//列表
	function getList(obj){
        var pgsize = pageSize;
		var sel = obj.dom;
        var pgi = obj.pgi || defaultIndex;
        var url = obj.url || host + "PointRule/List?pageIndex="+pgi+"&pageSize="+pgsize+"&m="+new Date().getTime();
        $kk.api.jsonp(url,function(res){
            $kk.$scope.ruleList = res.Records;
			
            for(var i=0;i<$kk.$scope.ruleList.length;i++){
               if(pgi > 1){
                   $kk.$scope.ruleList[i].order = i + (pgi-1)*pgsize + 1;
               }else{
                   $kk.$scope.ruleList[i].order = i + 1;
               }
            }
            $kk.fillData_new(sel);
            oa_common.makePageBar(function(pgindex){
               currentPageIndex = pgindex;
               buildPage(sel,pgindex,pgsize);
            },res.RecordsNumber,pgi,pgsize);
        });
    }
	
	function buildPage(sel,pgi,pgsize){
        searchRule(sel,keyword,pgi,pgsize);
    }

	function searchRule(sel,kw,pgi,pgsize){
        var kw = kw || "";
        keyword = kw;
        var pgi = pgi || defaultIndex;
        if(keyword.trim().length > 0){
            var pgsize = pageSize;
            var url = host + "PointRule/List?keyword=" + kw +"&pageIndex="+ pgi +"&pageSize="+pgsize+"&m="+new Date().getTime();
            getList({
                dom : sel,
                pgi : pgi,
                url : url,
                pgsize : pgsize
            })
        }else{
            getList({
                dom : sel,
                pgi : pgi,
                pgsize : pgsize
            });
        }
    }
	
	//添加
	function addRule(formdata,sel){
		var url = host + "PointRule/Create";
        $kk.api.post(url,formdata,function(res){
			searchRule(sel,keyword,currentPageIndex);
        })
	}
	
	//修改
    function modifyRule(formdata,sel){
        var url = host + "PointRule/Edit";
		console.log(formdata);
        $kk.api.post(url,formdata,function(res){
            searchRule(sel,keyword,currentPageIndex);
        })
    }
	
	//删除
    function deleteRule(id,sel){
        var url = host + "PointRule/Delete?pointRuleKid="+id;
        $kk.api.post(url,function(res){
            searchRule(sel,keyword,currentPageIndex);
        })
    }
	
	return {
        getList : getList,
		searchRule : searchRule,
		addRule : addRule,
		modifyRule : modifyRule,
		deleteRule : deleteRule
    }
})();